const app = getApp();
import { countdown } from '../../../utils/util.js'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsInfo: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgUrl: app.data.imgurl,
    hiddenModal: true,
    limitShow: false,
    limitTxt: '', 
    limitHours: '00',
    limitMin: '00',
    limitSecond: '00',
    groupList: [],
  },

  ready:function(){
    let { goodsInfo } = this.data;
    goodsInfo.FightGroupInfos.forEach((item, index) => {
      let endDate = countdown(item.EndTime);
      item.limitHours = endDate.limitHours;
      item.limitMin = endDate.limitMin;
      item.limitSecond = endDate.limitSecond;
      item.index = index;
    })
    this.setData({ groupList: goodsInfo.FightGroupInfos })
    setInterval(() =>{
      this.initData()
    },1000)
  },

  /**
   * 组件的方法列表
   */
  methods: {
    initData:function(){
      let { FightGroupActivityInfo, FightGroupInfos } = this.data.goodsInfo;
      let startState = countdown(FightGroupActivityInfo.StartDate);
      let endState = countdown(FightGroupActivityInfo.EndDate);
      if (startState.overTime) {
        this.setData({
          limitTxt: '距结束',
          limitHours: endState.limitHours,
          limitMin: endState.limitMin,
          limitSecond: endState.limitSecond
        })
      } else {
        this.setData({
          limitTxt: '距开始',
          limitHours: startState.limitHours,
          limitMin: startState.limitMin,
          limitSecond: startState.limitSecond
        })
      }

    },
    
    //立即拼团
    joinGroup:function(e){
      let { id, isown, sku } = e.currentTarget.dataset; 
      if (isown){
        wx.showToast({
          icon: 'none',
          title: '不能参加自己的团'
        })
        return
      }
      wx.navigateTo({
        url: `../confirmationOfOrder/confirmationOfOrder?fromPage=fightgroup&sku=${sku}&buyAmount=1&groupId=${id}`,
      })
    }
  }
})
